﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lilium.Web.Mvc.EntityFilters
{
	[Obsolete("Использовать IFilteringService")]
	public sealed class EntityFilterProvider: IEntityFilterProvider
	{
		public EntityFilterProvider(IServiceProvider serviceProvider, IEnumerable<IEntityFilterDescriptor> filterDescriptors)
		{
			if (serviceProvider == null) throw new ArgumentNullException("serviceProvider");
			if (filterDescriptors == null) throw new ArgumentNullException("filterDescriptors");

			m_FilterDescriptors = filterDescriptors.GroupBy(d => d.EntityType).ToDictionary(
				g => g.Key, 
				g => (IEnumerable<IEntityFilterDescriptor>)g.OrderBy(i => i.Order));
			m_ServiceProvider = serviceProvider;
		}

		#region IEntityFilterProvider Members

		public IEnumerable<IEntityFilter<TEntity>> GetEntityFilters<TEntity>()
		{
			IEnumerable<IEntityFilterDescriptor> filterDescriptors;
			if (FilterDescriptors.TryGetValue(typeof(TEntity), out filterDescriptors))
			{
				return filterDescriptors.Select(d => (IEntityFilter<TEntity>)d.CreateFilter(ServiceProvider));
			}
			else
				return Enumerable.Empty<IEntityFilter<TEntity>>();
		}

		#endregion

		#region private IDictionary<Type, IEntityFilterDescriptor> FilterDescriptors

		private readonly IDictionary<Type, IEnumerable< IEntityFilterDescriptor>> m_FilterDescriptors;

		private IDictionary<Type, IEnumerable< IEntityFilterDescriptor>> FilterDescriptors
		{
			get
			{
				return m_FilterDescriptors;
			}
		}

		#endregion

		#region private IServiceProvider ServiceProvider

		private readonly IServiceProvider m_ServiceProvider;

		private IServiceProvider ServiceProvider
		{
			get
			{
				return m_ServiceProvider;
			}
		}

		#endregion
	}
}
